﻿using Microsoft.Extensions.Logging;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using XeMail.Controls.Views;
using XeMail.Common.Helpers;
using XeMail.Common.User;
using XeMail.Controls.Controls;
using XeMail.Interfaces;
using XeMail.Service;
using XeMail.Views;
using XeMail.ViewModels;

namespace XeMail
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        ILogger _logger;
        IUser _user;
        protected override Window CreateShell()
        {
            //UI线程未捕获异常处理事件
            this.DispatcherUnhandledException += OnDispatcherUnhandledException;
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
            //多线程异常
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

            return Container.Resolve<MainWindow>();
        }

        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            //通常全局异常捕捉的都是致命信息
            _logger.LogCritical($"{e.Exception.StackTrace},{e.Exception.Message}");
        }

        private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            _logger.LogCritical($"{e.Exception.StackTrace},{e.Exception.Message}");
        }

        private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            _logger.LogInformation($"{ex.StackTrace},{ex.Message}");

            //记录dump文件
            MiniDump.TryDump($"dumps\\Wemail_{DateTime.Now.ToString("HH-mm-ss-ms")}.dmp");
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //创建Nlog组件实例
            var factory = new NLog.Extensions.Logging.NLogLoggerFactory();
            _logger = factory.CreateLogger("NLog");
            //注入到Prism DI容器中
            containerRegistry.RegisterInstance(_logger);

            //注册服务、依赖、View
            containerRegistry.Register<IMessageService, MessageService>();

            containerRegistry.RegisterDialog<UserLoginView, UserLoginViewModel>();

            _user = new UserModel();
            containerRegistry.RegisterInstance<IUser>(_user);

            //注册Dialog窗体
            containerRegistry.RegisterDialog<MessageDialogView, MessageDialogControl>();
        }

        /// <summary>
        /// 配置区域适配
        /// </summary>
        /// <param name="regionAdapterMappings"></param>
        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            //new ConfigurationModuleCatalog()

            //_logger.LogInformation($"读取Dll,反射获取名称。");
            //指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)
            return new DirectoryModuleCatalog() { ModulePath = @".\Apps" };
        }
    }
}
